﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Implementation of the Prototype Factory Pattern (for Command).
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FileSystem;
using FileSystem.Utility;

namespace FileSystem.Impl
{
	class ActionPrototypeFactory : IActionFactory
	{
		private static ActionPrototypeFactory instance;
        private List<IAction> allAction;

		private ActionPrototypeFactory()
		{
            allAction = new List<IAction>();
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("cwd"));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("END"));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("find", new object[] { "" }));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("find-async", new object[]{""}));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("print", new object[] { "" }));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("help"));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("ls"));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("mgr"));
            allAction.Add(ServiceLocator.Current.Resolve<IAction>("root-ls-all"));
		}

		public static ActionPrototypeFactory getInstance()
		{
			if (null == instance) {
				instance = new ActionPrototypeFactory();

			}
			return instance;
		}
	
		public ICollection<String> getNames() {

            return allAction.Select(x => x.Description.Name).ToList() ;
		}

		public Boolean hasName(String name) {
            return allAction.Any(x => x.Description.Name == name);
		}

		public IAction create(String name) {
            IAction p =  ServiceLocator.Current.Resolve<IAction>(name);
			if (null == p) {
				throw new ArgumentException("Factory does not have class '" + name + "'.");
			}
            return p;
		}

		public IAction create(String name, String initializer) {
            IAction p = ServiceLocator.Current.Resolve<IAction>(name, new object[]{initializer});
            if (null == p)
            {
                throw new ArgumentException("Factory does not have class '" + name + "'.");
            }
            return p;
            
		}

        #region IActionPrototypeRegistry Members


        public void describeAction(IAcionHelpStrategy actionHelpStrategy)
        {
            foreach (var action in allAction)
            {
                actionHelpStrategy.Visit(action.Description);
            }
        }

        #endregion
    }
}
